import { _decorator, Component, Node, Label, find, Animation, Sprite, SpriteFrame } from 'cc';
import { TopicManger } from './TopicManger'
const { ccclass, property } = _decorator;

@ccclass('scoreMgr')
export class scoreMgr extends Component {

    @property({ type: SpriteFrame })
    public comboImg1: SpriteFrame | null = null;

    @property({ type: SpriteFrame })
    public comboImg2: SpriteFrame | null = null;

    @property({ type: SpriteFrame })
    public comboImg3: SpriteFrame | null = null;

    @property({ type: SpriteFrame })
    public comboImg4: SpriteFrame | null = null;

    public static fenshuRef: Node = null // 当前分数
    public static curScoreRef: Node = null // 当前分数
    public static comboNumRef: Node = null // 加分
    public static kuaisuRef: Node = null // 快速秒答
    public static comboImgBoxRef: Node = null // 快速秒答
    public static comboImgRef: Sprite = null

    public static comboImgBoxList = []



    start() {
        scoreMgr.fenshuRef = find("Canvas/fenshu")
        scoreMgr.curScoreRef = find("Canvas/fenshu/curScore")
        scoreMgr.comboNumRef = find("Canvas/fenshu/comboNum")
        scoreMgr.kuaisuRef = find("Canvas/fenshu/kuaisu")
        scoreMgr.comboImgBoxRef = find("Canvas/fenshu/comboImgBox")

        scoreMgr.comboImgBoxList = [this.comboImg1, this.comboImg2, this.comboImg3, this.comboImg4]
        this.hideFenshuModal()


        console.log(scoreMgr.comboImgRef, 'scoreMgr.comboImgBoxRe111111111111f')
    }

    update(deltaTime: number) {

        
    }

    hideFenshuModal() {
        scoreMgr.curScoreRef.active = false
        scoreMgr.comboNumRef.active = false
        scoreMgr.kuaisuRef.active = false
        scoreMgr.comboImgBoxRef.active = false
    }

    // 更新页内分数和连击
    showFenshuModal() {
        if (TopicManger.curScore > 0) {
            scoreMgr.curScoreRef.getComponent(Label).string = `${TopicManger.curScore}`
            scoreMgr.curScoreRef.active = true
        }
        if (TopicManger.combo > 0) {
            scoreMgr.kuaisuRef.getComponent(Label).string = `秒答 x ${TopicManger.combo}`
            scoreMgr.kuaisuRef.active = true
            // tween(find("Canvas/fenshu/comboNum")).to(0.5, { position: v3(170, 20, 0) }).start();
        } else {
            scoreMgr.kuaisuRef.active = false
        }

        if (TopicManger.comboNum > 0) {
            scoreMgr.comboNumRef.getComponent(Label).string = `+ ${TopicManger.comboNum}`
            scoreMgr.comboNumRef.active = true
        }

        this.handleComboImgBoxRef(TopicManger.comboImgUrl)
        scoreMgr.fenshuRef.getComponent(Animation).play()
    }

    handleComboImgBoxRef(comboImgUrl) {
        console.log(comboImgUrl, 'comboImgUrl')
        switch (comboImgUrl) {
            case 'comboImg1':
                scoreMgr.comboImgBoxRef.getComponent(Sprite).spriteFrame = scoreMgr.comboImgBoxList[0]
                scoreMgr.comboImgBoxRef.active = true
                break;
            case 'comboImg2':
                scoreMgr.comboImgBoxRef.getComponent(Sprite).spriteFrame = scoreMgr.comboImgBoxList[1]
                scoreMgr.comboImgBoxRef.active = true
                break;
            case 'comboImg3':
                scoreMgr.comboImgBoxRef.getComponent(Sprite).spriteFrame = scoreMgr.comboImgBoxList[2]
                scoreMgr.comboImgBoxRef.active = true
                break;
            case 'comboImg4':
                scoreMgr.comboImgBoxRef.getComponent(Sprite).spriteFrame = scoreMgr.comboImgBoxList[3]
                scoreMgr.comboImgBoxRef.active = true
                break;
            default:
                scoreMgr.comboImgBoxRef.active = false
                break;
        }
    }
}

